﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Diagnostics;

namespace Webbshop.Account
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);

            GetAuthenticationMessage();
        }

        /// <summary>
        /// Om användaren blivit omdirigerad hit så visas ett felmeddelande upp.
        /// </summary>
        protected void GetAuthenticationMessage()
        {
            // No Admin-rights
            if (FormsAuthentication.GetRedirectUrl(User.Identity.Name, false).ToLowerInvariant() == "/ordersadmin.aspx" ||
                FormsAuthentication.GetRedirectUrl(User.Identity.Name, false).ToLowerInvariant() == "/stockstatus.aspx" ||
                FormsAuthentication.GetRedirectUrl(User.Identity.Name, false).ToLowerInvariant() == "/administrator.aspx.aspx")
            {
                if (Request.Cookies["Culture"].Value == "sv-SE")
                    lblAuthError.Text = "Du är inte inloggad som Administratör!<br/>";
                else
                    lblAuthError.Text = "You are not logged in as an Administrator!<br/>";
            }

            // Not logged in
            if (FormsAuthentication.GetRedirectUrl(User.Identity.Name, false).ToLowerInvariant() == "/checkout.aspx" ||
                FormsAuthentication.GetRedirectUrl(User.Identity.Name, false).ToLowerInvariant() == "/orders.aspx")
            {
                if (Request.Cookies["Culture"].Value == "sv-SE")
                    lblAuthError.Text = "Du är inte inloggad!<br/>";
                else
                    lblAuthError.Text = "You are not logged in!<br/>";
            }

            // If Page is rendered again, don't show the error message again.
            if (IsPostBack)
                lblAuthError.Text = "";
        }
    }
}
